<template>
  <div class='article-container'>
    <el-card class="box-card">
      <!-- 标题 -->
      <div slot="header" class="clearfix">
        <my-breadcrumb>评论管理</my-breadcrumb>
      </div>

      <!-- 主体表格 -->
      <el-table
        v-loading="loading"
        :data="comment"
        style="width: 100%">
        <el-table-column
          prop="title"
          label="文章标题"
        >
        </el-table-column>

        <el-table-column
          prop="total_comment_count"
          label="总评论数"
        >
        </el-table-column>

        <el-table-column
          prop="fans_comment_count"
          label="粉丝评论数"
        >
        </el-table-column>

        <!-- 由于后端回传的数据是数值，而我们要显示出来是对应字符串，
        所以，这里不能直接使用status -->
        <el-table-column
          label="评论状态"
        >
            <template slot-scope="scope">
          <el-tag v-if="scope.row.comment_status" type="success">正常</el-tag>
          <el-tag v-else type="error">关闭</el-tag>
        </template>
        </el-table-column>

        <el-table-column
          label="操作">
          <template slot-scope="scope">
        <el-button size="mini" v-if="scope.row.comment_status" type="danger" @click="hToggleStatus(scope.row)">关闭评论</el-button>
          <el-button size="mini" v-else type="primary" @click="hToggleStatus(scope.row)">打开评论</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination
        :disabled="loading"
        style="margin-top:10px;"
        background
        layout="prev, pager, next"
        @current-change="hPageChange"
        :page-size="10"
        :total="total_count">
      </el-pagination>

    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      total_count: 0, // 总数
      comment: [],
      page: 1,
      loading: false
    }
  },
  created () {
    this.loadComments()
  },
  methods: {
    // 发送请求获取数据渲染页面
    async loadComments () {
      try {
        const res = await this.$http({
          url: '/mp/v1_0/articles',
          method: 'GET',
          params: {
            page: this.page, // 当前页
            per_page: 20, // 一页的条数
            response_type: 'comment'
          }
        })
        this.total_count = res.data.data.total_count
        this.comment = res.data.data.results
      } catch (err) {
        console.log(err)
      }
    },
    // 分页功能
    hPageChange (page) {
      this.page = page
      this.loadComments()
    },
    // 点击改变评论状态功能
    async hToggleStatus (row) {
      try {
        this.loading = true
        const res = await this.$http({
          url: '/mp/v1_0/comments/status',
          method: 'PUT',
          params: {
            article_id: row.id.toString()
          },
          data: {
            allow_comment: !row.comment_status
          }
        })
        console.log(res)
        this.loading = false
        row.comment_status = !row.comment_status
        this.$message.success('操作成功')
      } catch (err) {
        console.log(err)
        this.loading = false
        this.$message.error('操作失败')
      }
    }
  }
}
</script>
